<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 设置头像布局 -->
      <div class="avator_box">
        <img src="../assets/avator.jpg" alt="" />
      </div>

      <!-- 设置输入框布局 -->
      <el-form
        label-width="0px"
        class="login_form"
        :model="loginForm"
        :rules="formRules"
        ref="loginFormRef"
      >
        <!--用户名-->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-account-fill"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            prefix-icon="iconfont icon-password"
          ></el-input>
        </el-form-item>

        <!-- 按钮 -->
        <el-form-item class="buttons">
          <el-button type="primary" @click="loginCheck">登陆</el-button>
          <el-button type="info" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456"
      },
      formRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 16, message: "长度在 6 到 16 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods:{
      resetForm(){

        //   console.log(this)
        this.$refs.loginFormRef.resetFields();
      },
      loginCheck(){
        this.$refs.loginFormRef.validate((valid)=>{
          // 这个就是卫语句，如果不符合就不向下执行了
          if ( !valid ){
            return;
          }

         this.$http.post("login",this.loginForm).then(resp => {
           
           const data = resp.data;
           console.log(data);
           
           if (data.meta.status == 200)
           {
              this.$message.success("登陆成功!")
              // 1.将登陆成功过后的token保存到本地的 sessionStorage 中
              //     1.1 项目中的其他接口必须在登陆接口认证过后才能访问
              //     1.1 token 只应该在当前网站打开期间生效，所以将session存放到sessionStorage中
              window.sessionStorage.setItem("token",data.data.token)

              // 2.通过编程式导航跳转到后台主页，路由地址是 /home
              this.$router.push("/home")

           } else {
              this.$message.error("登陆失败!")
           }

         });
         

          
        })
      }
  }
};
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}

.login_box {
  background-color: #fff;
  width: 450px;
  height: 300px;
  border-radius: 3px;
  /*设置绝对布局 */
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avator_box {
    width: 130px;
    height: 130px;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 10px;
    border: 1px solid #eee;
    border-radius: 50%;
    box-shadow: 0 0 10px #ddd;
    background-color: #fff;

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
    }
  }

  .login_form {
    position: absolute;
    width: 100%;
    bottom: 0px;
    padding: 0px 10px;
    box-sizing: border-box;
  }

  .buttons {
    display: flex;
    justify-content: flex-end;
  }
}</style
>>
